home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.10 Oct 88 / TransferSources / Introduction < prev    next >
Encoding:
Text File  |  1988-05-22  |  5.3 KB  |  126 lines  |  [TEXT/MPS ]

  1. The Transfer DA is a small desk accessory that adds a Transfer menu
  2. to any application that supports desk accessories.
  3.  
  4. When Apple killed the Minifinder, my work habits were shattered.  I
  5. tried Oasis, a Finder substitute, for a while but I didn't like the
  6. way the working directory was changed by launching a program; that
  7. is, if I launched, say, Macwrite, and opened a file, the sfget
  8. dialog came up showing the directory that contained Macwrite, rather
  9. than the one I had been working in.  I had to navigate through
  10. folders to return to my work files.
  11.  
  12. Transfer solves that problem.  Now, I work on the desktop but when I
  13. want to switch from one application to another, I use Transfer.  The
  14. working directory is unaffected, and I skip the Finder.
  15.  
  16. This DA illustrates some interesting programming techniques:  it is
  17. a segmented DA; it does an HFS search of an entire hard disk; it
  18. uses the iaznotify hook; and it creates resources with TML Pascal (a
  19. subject which came up in the May Mac Tutor letters column).
  20.  
  21. How the DA Works
  22. ----------------
  23.  
  24. When the DA is launched for the first time, it starts at the root of
  25. the disk which contains the System folder, and does an HFS catalog
  26. of the entire disk.  It finds each application on the disk, and
  27. stores the name and directory id in a special resource FILE called
  28. “Transfer data”, in the System folder.  Henceforth, it uses this
  29. file to build the menu (the HFS catalog takes a little time - about
  30. 30 seconds on my 20 meg hard disk - so you wouldn't want to go
  31. through it every time).
  32.  
  33. When an application is selected from the Transfer menu, the
  34. applications's name and directory id are copied into the DA's global
  35. data record, and the address of a special launch routine is
  36. installed in the “iaznotify” low-memory global.  The transfer may
  37. be immediate or delayed; in the first case, the DA will call
  38. “ExitToShell”; in the second, it will wait for the application to
  39. quit normally.
  40.  
  41. When the application quits, the Mac will prepare to launch the next
  42. application (usually the Finder).  As one of the last steps in this
  43. process, it will call “InitApplZone” to initialize the heap.  This
  44. trap will, in turn, call the routine whose address is stored in
  45. “iaznotify” - the DA's launch routine.  This routine simply writes
  46. the name of the desired application to “curappname” (another low-
  47. memory global), and sets the directory to the proper directory.
  48. The Mac will then go ahead and launch the application.  Sort of a
  49. cowbird launch, you might say!
  50.  
  51. DA segmentation
  52. ---------------
  53.  
  54. This is a small DA, a bit over 6K.  so why is it segmented?  Am I
  55. just showing off?
  56.  
  57. Before “InitApplZone” calls the launch procedure, the Mac closes all
  58. resource files.  That means that if the launch procedure is in a
  59. resource (e.g., the DRVR resource), it will be left in a free block.
  60. I don't feel very secure when executing code in a free block!  The
  61. solution is to detach the resource; the block will remain allocated
  62. until “InitApplZone” actually cleans up the heap - which happens
  63. after it calls the launch routine.
  64.  
  65. But if the launch routine is part of the DRVR resource, this means
  66. detaching the DRVR.  If we do that, we'll never know about menu
  67. selections, since the desk Manager needs a DRVR resource to pass DA
  68. events to.  so the launch routine has to be in a separate segment.
  69.  
  70. The launch routine is in a PACK resource.  The DRVR's “open” routine
  71. loads and locks the PACK, and records its address.  when it wants to
  72. call a routine from the PACK, it puts a routine selector into
  73. register D0 and jumps to the PACK.  At the top of the PACK is a jump
  74. table that branches to the routine corresponding to the selector.
  75.  
  76. There are other ways to segment a DA; this is just the way I did
  77. this one.
  78.  
  79. Creating Resources with TML Pascal
  80. ----------------------------------
  81.  
  82. Since it came up in the May letters column, let me say a few words
  83. on this topic.  It's really simple, once you know the (undocumented)
  84. trick.  It's all in the link control file.
  85.  
  86. For example, here is a link Control FILE to Create an LDEF:
  87.  
  88.     !fastlist
  89.     /codetype LDEF 1001 'Fast List' 32
  90.     list
  91.     pas$library
  92.     macintfglue
  93.     /end
  94.  
  95. The first line is the linker's start point - the name of the
  96. main routine.  The second line defines the resource type.  This
  97. will be an LDEF, resource id 1001, named “Fast List”, purgeable.
  98. The next three lines are the object files to link, in the order
  99. you want them to be linked.  And the final line means just what
  100. it says.
  101.  
  102. The code itself should be compiled as a unit, and most resource
  103. types will require that the main routine appear at the very top
  104. of the resource.
  105.  
  106. The two resources in this program are linked a bit differently,
  107. the DRVR resource because the resource definition requires a
  108. DA header at the top, and the PACK resource because I want a
  109. jump table at the top.  The fact that I left out the first Line
  110. and got away with it suggests that it may not be necessary.
  111.  
  112. Using the DA
  113. ------------
  114.  
  115. I have tested this DA with every application on my hard disk (at the
  116. time I wrote it).  A number of application require the delayed
  117. transfer; for example, any program that uses scratch files.  I found
  118. only one that would not work at all.  Can you guess?  That's right -
  119. Microsoft Word.
  120.  
  121. Transfer vs. Multifinder
  122. ------------------------
  123.  
  124. I leave as an exercise for the reader the problem of making this DA
  125. work with the Multifinder.  A more fruitful project might be
  126. converting the DA to an FKEY.